package me.zhengjie.modules.system.domain

import cn.hutool.core.bean.BeanUtil
import cn.hutool.core.bean.copier.CopyOptions
import com.fasterxml.jackson.annotation.JsonTypeName
import me.zhengjie.common.utils.JacksonUtil
import me.zhengjie.modules.system.service.dto.DeptDTO
import me.zhengjie.modules.system.service.dto.DictDTO
import jakarta.persistence.*
import org.apache.commons.lang3.builder.ToStringBuilder
import org.springframework.data.redis.core.RedisHash


/**
 * @author kuki
 * @date 2022-07-21
 */
@Entity
@Table(name = "sys_city")
@JsonTypeName("City")
@RedisHash("city")
data class City (
    // 序号
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    val id: Int? = null,

    // 中文名称
    @Column(name = "name", nullable = false)
    val name: String? = null,

    // 英文名称
    @Column(name = "name_en")
    val nameEn: String? = null,

    // 省份名称
    @Column(name = "name_chs", nullable = false)
    val nameChs: String? = null,

    //电话区码
    @Column(name = "area_code", nullable = false)
    val areaCode: String? = null,

    //邮政编码
    @Column(name = "zip_code", nullable = false)
    private val zipCode: String? = null,

    // 代码
    @Column(name = "city_code", nullable = false)
    val cityCode: Long? = null,

    // 省份
    @Column(name = "province_code", nullable = false)
    val provinceCode: Long? = null
) {
    annotation class Update

    companion object {
        fun fromJson(json: String): City? {
            val obj = JacksonUtil.parseObject(json, City::class.java)!!
            return when (obj) {
                is City -> obj
                else -> null
            }
        }
    }

    fun tojson(): String? {
        return JacksonUtil.toJSONString(this)
    }

    fun copy(source: City?) {
        BeanUtil.copyProperties(source, this, CopyOptions.create().setIgnoreNullValue(true))
    }
}